﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using Continental.Code.Entity;
using Continental.Code.DAL;

namespace Continental.Code.BL
{
    public class TuDiemProvider
    {
        #region Nhóm Tụ Điểm
        public static DataSet GetNhomTuDiemByParentID( Int32 parentID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNhomTuDiemListByParentID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ParentID", parentID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            return dsResult;
        }

        public static DataSet GetNhomTuDiemList()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNhomTuDiemList";
            command.CommandType = CommandType.StoredProcedure;
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            return dsResult;
        }

        public static NhomTuDiem GetNhomTuDiemByID( Int32 nhomID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNhomTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NhomID", nhomID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            NhomTuDiem theNhomTuDiem = null;

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                theNhomTuDiem = new NhomTuDiem();
                theNhomTuDiem.NhomID = Int32.Parse(dsResult.Tables[0].Rows[0]["NhomID"].ToString());
                theNhomTuDiem.TitleOrName = dsResult.Tables[0].Rows[0]["TitleOrName"].ToString();
                theNhomTuDiem.AmountOfChild = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfChild"].ToString());
                theNhomTuDiem.AmountOfTuDiem = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfTuDiem"].ToString());
                theNhomTuDiem.ParentID = Int32.Parse(dsResult.Tables[0].Rows[0]["ParentID"].ToString());
                theNhomTuDiem.DeletedFlag = Boolean.Parse(dsResult.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theNhomTuDiem.NumberOfOrder = Int32.Parse(dsResult.Tables[0].Rows[0]["NumberOfOrder"].ToString());
            }

            return theNhomTuDiem;
        }

        public static void InserNhomTuDiem( NhomTuDiem theNhomTuDiem)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_InsertNhomTuDiem";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TitleOrName", theNhomTuDiem.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theNhomTuDiem.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@ParentID", theNhomTuDiem.ParentID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNhomTuDiemByID( NhomTuDiem theNhomTuDiem)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNhomTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NhomID", theNhomTuDiem.NhomID));
            command.Parameters.Add(new SqlParameter("@TitleOrName", theNhomTuDiem.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theNhomTuDiem.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@ParentID", theNhomTuDiem.ParentID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteNhomTuDiemByID( Int32 nhomID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteNhomTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NhomID", nhomID));
            DataProvider.ExecuteNoneQuery(command);
        }

        #endregion

        #region Tụ Điểm

        public static void InsertTuDiem( Code.Entity.TuDiem theTuDiem)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_InsertTuDiem";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter tuDiemIDParam = new SqlParameter("@TuDiemID", SqlDbType.Int);
            tuDiemIDParam.Direction = ParameterDirection.Output;

            command.Parameters.Add(tuDiemIDParam);
            command.Parameters.Add(new SqlParameter("@NhomID", theTuDiem.NhomID));
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", theTuDiem.QuanHuyenID));
            command.Parameters.Add(new SqlParameter("@DuongPhoID", theTuDiem.DuongPhoID));
            command.Parameters.Add(new SqlParameter("@UserId", theTuDiem.UserId));
            command.Parameters.Add(new SqlParameter("@TitleOrName", theTuDiem.TitleOrName));
            command.Parameters.Add(new SqlParameter("@ShortDescription", theTuDiem.ShortDescription));
            command.Parameters.Add(new SqlParameter("@Description", theTuDiem.Description));
            command.Parameters.Add(new SqlParameter("@Address", theTuDiem.Address));
            command.Parameters.Add(new SqlParameter("@ToaDo", theTuDiem.ToaDo));
            command.Parameters.Add(new SqlParameter("@NumberPhone", theTuDiem.NumberPhone));
            command.Parameters.Add(new SqlParameter("@MobiPhone", theTuDiem.MobiPhone));
            command.Parameters.Add(new SqlParameter("@Email", theTuDiem.Email));
            command.Parameters.Add(new SqlParameter("@Website", theTuDiem.Website));
            command.Parameters.Add(new SqlParameter("@Name", theTuDiem.Name));
            command.Parameters.Add(new SqlParameter("@DescriptionSign", theTuDiem.DescriptionSign));
            command.Parameters.Add(new SqlParameter("@DescriptionNotSign", theTuDiem.DescriptionNotSign));
            command.Parameters.Add(new SqlParameter("@ImageUrl", theTuDiem.ImageUrl));
            command.Parameters.Add(new SqlParameter("@IsVip", theTuDiem.IsVip));
            command.Parameters.Add(new SqlParameter("@LevelOfRanging", theTuDiem.LevelOfRanging));
            command.Parameters.Add(new SqlParameter("@DeletedFlag", theTuDiem.DeletedFlag));
            DataProvider.ExecuteNoneQuery(command);
            theTuDiem.TuDiemID = Int32.Parse(command.Parameters["@TuDiemID"].Value.ToString());
        }

        public static Code.Entity.TuDiem GetTuDiemByID( Int32 tuDiemID)
        {
            Code.Entity.TuDiem theTuDiem = null;
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TuDiemID", tuDiemID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
      
            if (dsResult.Tables[0].Rows.Count > 0)
            {
                theTuDiem = new Code.Entity.TuDiem();
                theTuDiem.Address = dsResult.Tables[0].Rows[0]["Address"].ToString();
                theTuDiem.AmountOfBaoViPham = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfBaoViPham"].ToString());
                theTuDiem.AmountOfClick = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfClick"].ToString());
                theTuDiem.AmountOfDisLike = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfDisLike"].ToString());
                theTuDiem.AmountOfLike = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfLike"].ToString());
                theTuDiem.AmountOfNews = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfNews"].ToString());
                theTuDiem.AmountOfPhanHoi = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfPhanHoi"].ToString());
                theTuDiem.CityID = Int32.Parse(dsResult.Tables[0].Rows[0]["CityID"].ToString());
                theTuDiem.DateCreated = DateTime.Parse(dsResult.Tables[0].Rows[0]["DateCreated"].ToString());
                theTuDiem.Description = dsResult.Tables[0].Rows[0]["Description"].ToString();
                theTuDiem.DescriptionNotSign = dsResult.Tables[0].Rows[0]["DescriptionNotSign"].ToString();
                theTuDiem.DuongPhoID = Int32.Parse(dsResult.Tables[0].Rows[0]["DuongPhoID"].ToString());
                theTuDiem.Email = dsResult.Tables[0].Rows[0]["Email"].ToString();
                theTuDiem.ImageUrl = dsResult.Tables[0].Rows[0]["ImageUrl"].ToString();
                theTuDiem.IsVip = Boolean.Parse(dsResult.Tables[0].Rows[0]["IsVip"].ToString());
                theTuDiem.DeletedFlag = Boolean.Parse(dsResult.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theTuDiem.LevelOfRanging = Int32.Parse(dsResult.Tables[0].Rows[0]["LevelOfRanging"].ToString());
                theTuDiem.MobiPhone = dsResult.Tables[0].Rows[0]["MobiPhone"].ToString();
                theTuDiem.Name = dsResult.Tables[0].Rows[0]["Name"].ToString();
                theTuDiem.NhomID = Int32.Parse(dsResult.Tables[0].Rows[0]["NhomID"].ToString());
                theTuDiem.NumberPhone = dsResult.Tables[0].Rows[0]["NumberPhone"].ToString();
                theTuDiem.QuanHuyenID = Int32.Parse(dsResult.Tables[0].Rows[0]["QuanHuyenID"].ToString());
                theTuDiem.ShortDescription = dsResult.Tables[0].Rows[0]["ShortDescription"].ToString();
                theTuDiem.TitleOrName = dsResult.Tables[0].Rows[0]["TitleOrName"].ToString();
                theTuDiem.ToaDo = dsResult.Tables[0].Rows[0]["ToaDo"].ToString();
                theTuDiem.TuDiemID = Int32.Parse(dsResult.Tables[0].Rows[0]["TuDiemID"].ToString());
                theTuDiem.UserId = dsResult.Tables[0].Rows[0]["UserId"].ToString();
                theTuDiem.Website = dsResult.Tables[0].Rows[0]["Website"].ToString();
            }

            return theTuDiem;
        }

        public static void UpdateTuDiemByID( Code.Entity.TuDiem theTuDiem)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TuDiemID", theTuDiem.TuDiemID));
            command.Parameters.Add(new SqlParameter("@NhomID", theTuDiem.NhomID));
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", theTuDiem.QuanHuyenID));
            command.Parameters.Add(new SqlParameter("@DuongPhoID", theTuDiem.DuongPhoID));
            command.Parameters.Add(new SqlParameter("@UserId", theTuDiem.UserId));
            command.Parameters.Add(new SqlParameter("@TitleOrName", theTuDiem.TitleOrName));
            command.Parameters.Add(new SqlParameter("@ShortDescription", theTuDiem.ShortDescription));
            command.Parameters.Add(new SqlParameter("@Description", theTuDiem.Description));
            command.Parameters.Add(new SqlParameter("@Address", theTuDiem.Address));
            command.Parameters.Add(new SqlParameter("@ToaDo", theTuDiem.ToaDo));
            command.Parameters.Add(new SqlParameter("@NumberPhone", theTuDiem.NumberPhone));
            command.Parameters.Add(new SqlParameter("@MobiPhone", theTuDiem.MobiPhone));
            command.Parameters.Add(new SqlParameter("@Email", theTuDiem.Email));
            command.Parameters.Add(new SqlParameter("@Website", theTuDiem.Website));
            command.Parameters.Add(new SqlParameter("@Name", theTuDiem.Name));
            command.Parameters.Add(new SqlParameter("@DescriptionSign", theTuDiem.DescriptionSign));
            command.Parameters.Add(new SqlParameter("@DescriptionNotSign", theTuDiem.DescriptionNotSign));
            command.Parameters.Add(new SqlParameter("@ImageUrl", theTuDiem.ImageUrl));
            command.Parameters.Add(new SqlParameter("@IsVip", theTuDiem.IsVip));
            command.Parameters.Add(new SqlParameter("@LevelOfRanging", theTuDiem.LevelOfRanging));
            command.Parameters.Add(new SqlParameter("@DeletedFlag", theTuDiem.DeletedFlag));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetTuDiemListByFillter(   String titleOrName, 
                                                        Int32 nhomID, 
                                                        Int32 quanHuyenID, 
                                                        Int32 duongPhoID, 
                                                        Boolean deletedFlag)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTuDiemListByFillter";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TitleOrName", titleOrName));
            command.Parameters.Add(new SqlParameter("@NhomID", nhomID));
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            command.Parameters.Add(new SqlParameter("@DeletedFlag", deletedFlag));
            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetTuDiemListByFilterPaging(  Int32 nhomID, 
                                                            Int32 quanHuyenID, 
                                                            Int32 duongPhoID, 
                                                            Int32 pageIndex,
                                                            Int32 pageSize,
                                                            out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTuDiemListByFilterPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NhomID", nhomID));
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            return DataProvider.ExecuteDataSet(command, out totalRecord);
        }

        public static void DeleteTuDiemByID( Int32 tuDiemID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteTuDiemByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TuDiemID", tuDiemID));
            DataProvider.ExecuteNoneQuery(command);
        }
        #endregion
    }
}
